package com.probiz.estore.other.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import com.probiz.estore.common.model.other.Tag;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.core.util.I18nUtil;
import com.probiz.estore.core.util.StringUtil;
import com.probiz.estore.other.dao.TagDao;
import com.probiz.estore.other.service.TagManager;


/**
 * Manager implementation for Tag, responsible for business processing, and communicate between web and persistence layer.
 */
public class TagManagerImpl extends GenericManagerImpl<Tag> implements TagManager {

	private TagDao tagDao = null;

	/**
	 * @param tagDao
	 *            the tagDao to set
	 */
	public void setTagDao(TagDao tagDao) {
		this.tagDao = tagDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = tagDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(Tag entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onSave(java.lang.Object)
	 */
	@Override
	protected void preSave(Tag entity) {

	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.other.service.TagManager#saveFully(com.probiz.estore.common.model.other.Tag)
	 */
	public Map<String, Object> saveFully(Tag tag){
		Map<String, Object> returnMsgs = new HashMap<String, Object>();
		this.evict(tag);//必须 evict出session 否则会有update操作
		Tag existTag =  this.getTagByModuleByName(tag.getModule(), tag.getTagName());
		if(existTag!=null){
			if(!existTag.getTagId().equals(tag.getTagId())){
				//已有该tag
				returnMsgs.put("error", I18nUtil.getInstance().getMessage("tagDetail.tagName.repeated"));
				return returnMsgs;
			}
			this.evict(existTag);//必须 evict出session 否则会有update操作
			this.evict(tag);//必须 evict出session 否则会有update操作
		}
		this.save(tag);
		return returnMsgs;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.other.service.TagManager#getTagByModuleByName(java.lang.Short, java.lang.String)
	 */
	public Tag getTagByModuleByName(Short module,String name){
		return tagDao.getTagByModuleByName(module, name);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.other.service.TagManager#getAllTagByModule(java.lang.Short)
	 */
	public List<Tag> getAllTagByModule(Short module){
		return tagDao.listByProperty("module", module);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.other.service.TagManager#doSetTagField(java.lang.Object, java.lang.Short, java.lang.String)
	 */
	public Map<String,Object> doSetTagField(Object obj,Short module,String tagNames){
		Map<String,Object> returnData = new HashMap<String,Object>();
		List<String> tooLongTagNameList = new ArrayList<String>();
		String[] tagNameArray = tagNames.replaceAll("\\s+", ",").split(",");
		if(!ArrayUtils.isEmpty(tagNameArray)){
			Set<String> tagIdList = new HashSet<String>();
			for(String tagName: tagNameArray){
				tagName = tagName.trim();
				if(!tagName.equals("")){
					Tag tag =  this.getTagByModuleByName(module, tagName);
					if(tag == null){
						if(tagName.length() <= 30){
							tag = new Tag();
							tag.setModule(module);
							tag.setTagName(tagName);
							this.save(tag);
							tagIdList.add(tag.getTagId().toString());
							this.flush();
						}else{
							tooLongTagNameList.add(tagName);
						}
					}else{
						tagIdList.add(tag.getTagId().toString());
					}
				}	
			}
			try {
				BeanUtils.setProperty(obj, "tags", StringUtil.arrayToString(tagIdList.toArray(new String[0]),","));
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}else{
			try {
				BeanUtils.setProperty(obj, "tags", "");
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		if(tooLongTagNameList.size() > 0){
			returnData.put("error", I18nUtil.getInstance().getMessage("tagSelector.error.tooLong",new Object[]{StringUtil.arrayToString(tooLongTagNameList.toArray(new String[0]), ",")}));
		}
		return returnData;
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.other.service.TagManager#getTagNameArray(java.lang.Object)
	 */
	public String[] getTagNameArray(Object obj){
		String tags = null;
		try {
			tags = BeanUtils.getProperty(obj, "tags");
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		if(StringUtils.isNotBlank(tags)){
			List<String> tagNameArray = new ArrayList<String>();
			String[] tagIds = tags.split(",");
			for(String tagId:tagIds){
				Tag tag = this.getById(Integer.valueOf(tagId));
				if(null != tag){
					tagNameArray.add(tag.getTagName());
				}
			}
			return tagNameArray.toArray(new String[0]);
		}else{
			return null;
		}
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.other.service.TagManager#getTagNames(java.lang.Object)
	 */
	public String getTagNames(Object obj){
		String[] tagNameArray = getTagNameArray(obj);
		if(null == tagNameArray){
			return "";
		}else{
			return StringUtil.arrayToString(tagNameArray, " ");
		}
	}

}
